﻿using System;
using System.Collections.Generic;
using System.Configuration;
using SqlSugar;

namespace TestORM1.Model
{
    public class SqlSugarHelper
    {
        private static readonly string connectionString =
            ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
        //static SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
        //{
        //    ConnectionString = connectionString,//连接符字串
        //    DbType = DbType.SqlServer,
        //    IsAutoCloseConnection = true,
        //    InitKeyType = InitKeyType.Attribute//从特性读取主键自增信息
        //});
        public static SqlSugarClient db
        {
            get
            {
                return new SqlSugarClient(new ConnectionConfig()
                {
                    DbType = DbType.SqlServer,
                    ConnectionString = connectionString,
                    InitKeyType = InitKeyType.Attribute,
                    IsAutoCloseConnection = true
                });
            }
        }

        public static void Insert(Blog input)
        {
            db.Insertable<Blog>(input).ExecuteReturnIdentity();
        }
        public static void Insert(List<Blog> input)
        {
            db.Insertable<Blog>(input).ExecuteReturnIdentity();
        }

        public static Blog Get(int id)
        {
            return db.Queryable<Blog>().First(m=>m.Id==id);
        }
        /// <summary>
        /// ID 倒叙
        /// </summary>
        /// <param name="take"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static List<Blog> GetListOrberId(int take,int size=100)
        {
            int total=0;
            return db.Queryable<Blog>()
                .Where(m=>m.CTime<DateTime.Now)
                .OrderBy(m=>m.Id,OrderByType.Desc)
                .ToPageList(take/size,size,ref total);
        }
        /// <summary>
        /// 创建时间倒叙
        /// </summary>
        /// <param name="take"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static List<Blog> GetListOrberTime(int take,int size=100)
        {
            int total = 0;
            return db.Queryable<Blog>()
                .Where(m=>m.CTime<DateTime.Now)
                .OrderBy(m=>m.CTime,OrderByType.Desc)
                .ToPageList(take/size,size,ref total);
        }
    }
}